package gov.mmgt.netmanager.ui;

import gov.mmgt.netmanager.ui.util.UIUtil;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

public class SystemSettingDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Label closeBtn;
	private Composite wordArea;
	private Composite workAreaBottom;
	private Composite workAreaMain;
	private CLabel systemSettingMenu;
	private CLabel secureSettingMenu;
	private CLabel userManagerMenu;
	
	private Map<String,CLabel> menus;
	private Map<String,Composite> panles;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public SystemSettingDialog(Shell parent, int style) {
		super(parent, style);
		menus = new HashMap<String,CLabel>();
		panles = new HashMap<String, Composite>();
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.NO_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(555, 380);
		
		InputStream is = this.getClass().getResourceAsStream(
				"images/setting_title.png");
		shell.setBackgroundImage(new Image(Display.getDefault(), is));
		shell.setBackgroundMode(SWT.INHERIT_FORCE);
		
		// 窗口居中
		UIUtil.moveWinToMiddle(shell);
		
		// 鼠标拖动
		UIUtil.drag(shell);
		
		Label titleLable = new Label(shell, SWT.NONE);
		titleLable.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		titleLable.setBounds(10, 10, 61, 15);
		titleLable.setText("系统设置");
		
		//===============================关闭按钮===============================//
		closeBtn = new Label(shell, SWT.NONE);
		closeBtn.setToolTipText("关闭");
		closeBtn.setBounds(514, 0, 31, 21);
		is = this.getClass().getResourceAsStream("images/close_off.png");
		closeBtn.setImage(new Image(Display.getDefault(), is));
		
		closeBtn.addMouseTrackListener(new MouseTrackAdapter() {
			// 鼠标在按钮上的事件
			@Override
			public void mouseEnter(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/close_on.png");
				closeBtn.setImage(new Image(Display.getDefault(), is));
			}

			// 鼠标离开事件
			@Override
			public void mouseExit(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/close_off.png");
				closeBtn.setImage(new Image(Display.getDefault(), is));
			}
		});

		closeBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.close();
			}
		});
		//===============================关闭按钮===============================//
		
		//===============================主工作区背景===============================//
		workAreaMain = new Composite(shell, SWT.NONE);
		workAreaMain.setBounds(0, 34, 555, 345);
		is = this.getClass().getResourceAsStream("images/workArea_main.png");
		workAreaMain.setBackgroundImage(new Image(Display.getDefault(), is));
		
		workAreaBottom = new Composite(shell, SWT.NONE);
		workAreaBottom.setBounds(0, 379, 555, 1);
		is = this.getClass().getResourceAsStream("images/workArea_bottom.png");
		workAreaBottom.setBackgroundImage(new Image(Display.getDefault(), is));
		//===============================主工作区背景===============================//
		
		//===============================菜单栏===============================//
		Composite menuList = new Composite(workAreaMain, SWT.NONE);
		menuList.setBounds(1, 0, 166, 345);
		is = this.getClass().getResourceAsStream("images/menuList.png");
		menuList.setBackgroundImage(new Image(Display.getDefault(), is));
		//===============================菜单栏===============================//
		
		//===============================菜单项===============================//
		//系统设置
		systemSettingMenu = new CLabel(menuList, SWT.SHADOW_NONE);
		systemSettingMenu.setAlignment(SWT.CENTER);
		systemSettingMenu.setBounds(0, 10, 165, 31);
		systemSettingMenu.setText("系统设置");
		
		//默认“系统设置”菜单为选择项
		is = this.getClass().getResourceAsStream("images/tab_selected.png");
		systemSettingMenu.setBackgroundImage(new Image(Display.getDefault(), is));
		systemSettingMenu.setData("selected", true);
		systemSettingMenu.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		systemSettingMenu.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				if((Boolean)menus.get("systemSetting").getData("selected")) {
					return;
				}
				
				InputStream is = this.getClass().getResourceAsStream("images/tab_on.png");
				systemSettingMenu.setBackgroundImage(new Image(Display.getDefault(), is));
			}
			
			@Override
			public void mouseExit(MouseEvent e) {
				if((Boolean)menus.get("systemSetting").getData("selected")) {
					return;
				}
				
				InputStream is = this.getClass().getResourceAsStream("images/tab_off.png");
				systemSettingMenu.setBackgroundImage(new Image(Display.getDefault(), is));
			}
		});
		
		systemSettingMenu.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				tabSelected("systemSetting");
			}
		});
		
		menus.put("systemSetting",systemSettingMenu);
		
		//安全设置
		secureSettingMenu = new CLabel(menuList, SWT.SHADOW_NONE);
		secureSettingMenu.setAlignment(SWT.CENTER);
		secureSettingMenu.setBounds(0, 41, 165, 31);
		secureSettingMenu.setText("安全设置");
		is = this.getClass().getResourceAsStream("images/tab_off.png");
		secureSettingMenu.setBackgroundImage(new Image(Display.getDefault(), is));
		secureSettingMenu.setData("selected", false);
		
		secureSettingMenu.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				if((Boolean)menus.get("secureSetting").getData("selected")) {
					return;
				}
				
				InputStream is = this.getClass().getResourceAsStream("images/tab_on.png");
				secureSettingMenu.setBackgroundImage(new Image(Display.getDefault(), is));
			}
			
			@Override
			public void mouseExit(MouseEvent e) {
				if((Boolean)menus.get("secureSetting").getData("selected")) {
					return;
				}
				
				InputStream is = this.getClass().getResourceAsStream("images/tab_off.png");
				secureSettingMenu.setBackgroundImage(new Image(Display.getDefault(), is));
			}
		});
		
		secureSettingMenu.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				tabSelected("secureSetting");
			}
		});
		
		menus.put("secureSetting",secureSettingMenu);
		
		//用户管理
		userManagerMenu = new CLabel(menuList, SWT.SHADOW_NONE);
		userManagerMenu.setAlignment(SWT.CENTER);
		userManagerMenu.setBounds(0, 72, 165, 31);
		userManagerMenu.setText("用户管理");
		is = this.getClass().getResourceAsStream("images/tab_off.png");
		userManagerMenu.setBackgroundImage(new Image(Display.getDefault(), is));
		userManagerMenu.setData("selected", false);
		
		userManagerMenu.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				if((Boolean)menus.get("userManager").getData("selected")) {
					return;
				}
				
				InputStream is = this.getClass().getResourceAsStream("images/tab_on.png");
				userManagerMenu.setBackgroundImage(new Image(Display.getDefault(), is));
			}
			
			@Override
			public void mouseExit(MouseEvent e) {
				if((Boolean)menus.get("userManager").getData("selected")) {
					return;
				}
				
				InputStream is = this.getClass().getResourceAsStream("images/tab_off.png");
				userManagerMenu.setBackgroundImage(new Image(Display.getDefault(), is));
			}
		});
		
		userManagerMenu.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				tabSelected("userManager");
			}
		});
		
		menus.put("userManager",userManagerMenu);
		//===============================菜单项===============================//
		
		//===============================工作区===============================//
		wordArea = new Composite(workAreaMain, SWT.NONE);
		wordArea.setBounds(167, 0, 387, 345);
		wordArea.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		//===============================工作区===============================//
		
		//初始化工作版面
		panles.put("systemSetting", new SystemSettingPanle(wordArea,SWT.NONE));
		panles.put("secureSetting", new SecureSettingPanle(wordArea,SWT.NONE));
		panles.put("userManager", new UserManagerPanle(wordArea,SWT.NONE));
	}
	
	public void tabSelected(String tabName) {
		for(String key : menus.keySet()) {
			if((Boolean)menus.get(key).getData("selected")) {
				menus.get(key).setData("selected", false);
				InputStream is = this.getClass().getResourceAsStream("images/tab_off.png");
				menus.get(key).setBackgroundImage(new Image(Display.getDefault(), is));
				menus.get(key).setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
			}
		}
		
		menus.get(tabName).setData("selected", true);
		InputStream is = this.getClass().getResourceAsStream("images/tab_selected.png");
		menus.get(tabName).setBackgroundImage(new Image(Display.getDefault(), is));
		menus.get(tabName).setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		for(String key : panles.keySet()) {
			panles.get(key).setVisible(false);
		}
		
		panles.get(tabName).setVisible(true);
		
		wordArea.layout(true);
	}
}
